package me.kuye.jkuye.plugin.activerecord.dialect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import me.kuye.jkuye.plugin.activerecord.Model;
import me.kuye.jkuye.plugin.activerecord.Page;
import me.kuye.jkuye.plugin.activerecord.Record;
import me.kuye.jkuye.plugin.activerecord.Table;

public abstract class Dialect {
	public abstract String forTableBuilderDoBuild(String tableName);

	public abstract void forModelSave(Table table, Map<String, Object> attrs, StringBuilder sql, List<Object> paras);

	public abstract String forModelDeleteById(Table table);

	public abstract void forModelUpdate(Table table, Map<String, Object> attrs, Set<String> modifyFlag,
			StringBuilder sql, List<Object> paras);

	public abstract String forModelFindById(Table table, String columns);

	public abstract String forDbFindById(String tableName, String[] pKeys);

	public abstract String forDbDeleteById(String tableName, String[] pKeys);

	public abstract void forDbSave(StringBuilder sql, List<Object> paras, String tableName, String[] pKeys,
			Record record);

	public abstract void forDbUpdate(String tableName, String[] pKeys, Object[] ids, Record record, StringBuilder sql,
			List<Object> paras);

	public abstract void forPaginate(StringBuilder sql, int pageNumber, int pageSize, String select,
			String sqlExceptSelect);

	public boolean isOracle() {
		return false;
	}

	public boolean isTakeOverModelPaginate() {
		return false;
	}

	public Page<Record> takeOverDbPaginate(Connection conn, int pageNumber, int pageSize, String select,
			String sqlExceptSelect, Object... paras) throws SQLException {
		throw new RuntimeException("You should implements this method in " + getClass().getName());
	}

	@SuppressWarnings("rawtypes")
	public Page takeOverModelPaginate(Connection conn, Class<? extends Model> modelClass, int pageNumber, int pageSize,
			String select, String sqlExceptSelect, Object... paras) throws Exception {
		throw new RuntimeException("You should implements this method in " + getClass().getName());
	}

	public void fillStatement(PreparedStatement pst, List<Object> args) throws SQLException {
		for (int i = 0, size = args.size(); i < size; i++) {
			pst.setObject(i + 1, args.get(i));
		}
	}

	public void fillStatement(PreparedStatement pst, Object... args) throws SQLException {
		for (int i = 0; i < args.length; i++) {
			pst.setObject(i + 1, args[i]);
		}
	}
}
